package com.zzqstart.cateorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzqstart.cateorder.entity.CreateCondition;
import com.zzqstart.cateorder.entity.Order;
import com.zzqstart.cateorder.entity.OrderSearh;
import com.zzqstart.cateorder.service.OrderService;
import com.zzqstart.commonutils.JwtUtils;
import com.zzqstart.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-03-03
 */
@RestController
@RequestMapping("/cateorder/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/createorder")
    public R createOrder(HttpServletRequest request, @RequestBody CreateCondition createCondition) {

        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        String orderNo = orderService.createOrder(createCondition, memberId);

        return R.ok().data("orderId", orderNo);
    }

    @GetMapping("/getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderId);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("item", order);
    }

    @GetMapping("/getStatusByCourseIdAndMemberId/{recipeId}/{memberId}")
    public boolean getStatusByCourseIdAndMemberId(
            @PathVariable String recipeId,
            @PathVariable String memberId) {

        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id", memberId);
        wrapper.eq("recipe_id", recipeId);
        int count = orderService.count(wrapper);

        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    @PostMapping("getBackOrderInfo/{current}/{limit}")
    public R getBackOrderInfo(@PathVariable Long current, @PathVariable Long limit,@RequestBody(required = false) OrderSearh searchObj) {
        Map<String, Object> map = orderService.getBackOrderInfo(searchObj, current, limit);
        return R.ok().data(map);
    }

    @DeleteMapping("/deleteOrder/{orderId}")
    public R deleteOrder(@PathVariable String orderId,HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        Boolean isDel = orderService.deleteOrder(memberId,orderId);
        return R.ok().data("isDel",isDel);
    }
}

